package com.demo.commons.DTO;

import com.demo.commons.constant.ConStantsR;
import com.demo.commons.constant.HttpStatusR;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.Set;

/**
 * 返回类
 *@author zwy
 *@date 2023/8/9 4:25
*/
@Data
public class Result<T> {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty("信息描述")
    private static String msg;
    @ApiModelProperty("状态码")
    private static Integer code;
    @ApiModelProperty("返回数据")
    private T data;

    /**
     * @param type
     * @param <T>
     * @return 返回成功
     */
    public static <T> Result<T> rule(Class<T> type) {
        Result<T> tResult = new Result<>();
        tResult.setMsg(ConStantsR.SUCCESS);
        tResult.setCode(HttpStatusR.SUCCESS);
        return tResult;
    }

    public static <T> Result<List<T>> ruleList(Class<T> type) {
        Result<List<T>> tResult = new Result<>();
        tResult.setMsg(ConStantsR.SUCCESS);
        tResult.setCode(HttpStatusR.SUCCESS);
        return tResult;
    }

    public static <T> Result<Set<T>> ruleSet(Class<T> type) {
        Result<Set<T>> tResult = new Result<>();
        tResult.setMsg(ConStantsR.SUCCESS);
        tResult.setCode(HttpStatusR.SUCCESS);
        return tResult;
    }

    /**
     * 返回失败(默认)
     */
    public static <T> Result<T> error() {
        Result<T> tResult = new Result<>();
        tResult.setMsg(ConStantsR.ERROR);
        tResult.setCode(HttpStatusR.ERROR);
        return tResult;
    }
    /**
     * 返回失败
     */
    public static <T> Result<T> error(String msg) {
        Result<T> tResult = new Result<>();
        tResult.setMsg(msg);
        tResult.setCode(HttpStatusR.ERROR);
        return tResult;
    }
    /**
     * 返回失败
     */
    public static <T> Result<T> error(int code, String msg) {
        Result<T> tResult = new Result<>();
        tResult.setMsg(msg);
        tResult.setCode(code);
        return tResult;
    }
    public static <T> Result<T> rule() {
        Result<T> tResult = new Result<>();
        tResult.setMsg(ConStantsR.SUCCESS);
        tResult.setCode(HttpStatusR.SUCCESS);
        return tResult;
    }
    /**
     * 根据sql返回的行数判断是否执行成功
     */
    public Result RuleRow(Integer row) {
        return row > 0 ? rule() : error();
    }

    public String getMsg() {
        return msg;
    }

    public Integer getCode() {
        return code;
    }

    public Result<T> setCode(Integer code) {
        Result.code = code;
        return this;
    }

    public Result<T> setMsg(String msg) {
        Result.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }
}
